package com.java.androidmarket.controller;

import com.java.androidmarket.aspect.CheckUser;
import com.java.androidmarket.entity.Goods;
import com.java.androidmarket.entity.Result;
import com.java.androidmarket.entity.Star;
import com.java.androidmarket.service.StarService;
import com.java.androidmarket.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * description:
 * author:张腾
 * date:2021-04-27
 */

@RestController
@RequestMapping("star")
@CrossOrigin
@Api(tags = "收藏模块")
public class StarController {

    @Autowired
    private StarService starService;
    @Autowired
    private RedisUtils redisUtils;

    @GetMapping("getStarGoods")
    @ApiOperation(value = "获取收藏商品", notes = "根据用户id获取该用户收藏的商品")
    @ApiImplicitParam(dataType = "int", name = "userId", value = "用户ID")
    @CheckUser
    public Result getStarGoods(int userId){
        List<Goods> starGoods = new ArrayList<>();
        int total = 0;

        String key = "Star:"+userId;
        if (redisUtils.exists(key)){
            System.out.println("redis查找收藏");
            starGoods = (List<Goods>) redisUtils.hmGet(key,"StarGoods");
            total = (int) redisUtils.hmGet(key,"Total");
        }else{
            System.out.println("数据库查找收藏");
            starGoods = starService.getStarGoods(userId);
            total = starService.getStarCount(userId);

            redisUtils.hmSet(key,"StarGoods",starGoods);
            redisUtils.hmSet(key,"Total",total);
        }

        return Result.success(starGoods,total);
    }


    @GetMapping("ifStar")
    @ApiOperation(value = "判断是否已收藏" ,notes = "根据用户id和商品id判断用户是否收藏该商品")
    @ApiImplicitParam(dataType = "Star", name = "star", value = "star收藏实体类")
    public Result ifStar(Star star){
        Integer userId =  star.getUserId();
        Integer goodsId = star.getGoodsId();

        int i = starService.ifStar(userId, goodsId);
        if (i==1){
            return Result.success("已收藏");
        }else {
            return Result.success("未收藏");
        }

    }


    @PostMapping("add")
    @ApiOperation(value = "添加收藏" ,notes = "一个用户添加多个商品至收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "userId", value = "用户ID"),
            @ApiImplicitParam(dataType = "List<Integer>", name = "goodsIds", value = "商品ID数组")
    })
    public Result addStar(@RequestBody Map<String,Object> map){
        Integer userId = (Integer) map.get("userId");
        List<Integer> goodsIds = (List<Integer>) map.get("goodsIds");

        if (redisUtils.exists("Star:"+userId)){
            redisUtils.del("Star:"+userId);
        }
        starService.addStar(userId,goodsIds);
        return Result.success();

    }

    @PostMapping("delete")
    @ApiOperation(value = "删除收藏", notes = "根据用户id和商品id删除收藏")
    @ApiImplicitParam(dataType = "Star", name = "star", value = "star收藏实体类")
    public Result delStar(@RequestBody Star star){
        Integer userId =  star.getUserId();
        Integer goodsId = star.getGoodsId();

        if (redisUtils.exists("Star:"+userId)){
            redisUtils.del("Star:"+userId);
        }

        starService.delStar(userId,goodsId);

        return Result.success();
    }
}
